(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.currencyCalendar.deleteController', deleteController);
	deleteController.$inject = [ '$scope', 'Constants', 'modalService', 'layoutConfig', 'validatorConfig', 'gridService', 'authService', 'currencyCalendarService' ];
	function deleteController($scope, Constants, modalService, layoutConfig, validatorConfig, gridService, authService, currencyCalendarService) {
		var ctrl = this, currencyCalendarInfo = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.currencyCalendar = {};
		
		$scope.$watch('viewCalendarYear', function(newValue, oldValue) {
			currencyCalendarInfo.calendarDay.year = newValue;
			authService.view(currencyCalendarInfo).then(function(response) {				
				angular.merge(ctrl.currencyCalendar, response.data);
				$scope.calendarYear = ctrl.currencyCalendar.calendarDay.year;
			});
		});
				
		
		ctrl.init = function() {
			currencyCalendarService.initOptions($scope, ctrl).then(function() {
				authService.delete(currencyCalendarInfo).then(function(response) {
					ctrl.currencyCalendar = response.data || currencyCalendarInfo;
					$scope.calendarYear = ctrl.currencyCalendar.calendarDay.year;
					$scope.viewCalendarYear = ctrl.currencyCalendar.calendarDay.year;
					ctrl.currencyCalendar.calendarDay.yearList = ctrl.currencyCalendar.calendarDay.yearList ||[];
					if ( ctrl.currencyCalendar.calendarDay.yearList.length == 0) {	
						ctrl.currencyCalendar.calendarDay.yearList.push({year: ctrl.currencyCalendar.calendarDay.year});
					}
					$scope.yearList = ctrl.currencyCalendar.calendarDay.yearList;
				});
			});
		};
		
		ctrl.deleteCountryCalendar = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.delete', args : [ctrl.currencyCalendar.primaryKey]} ],
				'btnCallback' : {
					'ok' : function(response) {
						authService.deleteSubmit(ctrl.currencyCalendar).then(deleteSuccessful, deleteFailed);
					}
				}
			});
		};
		
		function deleteSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.delete' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.currencyCalendar.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.currencycalendar.list',
					'state' : 'cda.currencyCalendar.list',
					'params' : {}
				}]
			});
		}

		function deleteFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
	}
})(angular);